<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.groupName" placeholder="名称" clearable style="width: 250px;" class="filter-item"
        @keyup.enter.native="handleFilter" />
      <el-select v-model="listQuery.isDeleted" placeholder="状态" clearable style="width: 110px" class="filter-item">
        <el-option label="正常" :value="0" />
        <el-option label="已删除" :value="1" />
      </el-select>
      &nbsp;
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        查询
      </el-button>
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit"
        @click="handleCreate">
        添加
      </el-button>
      <!-- <el-button v-waves :loading="downloadLoading" class="filter-item" type="primary" icon="el-icon-download"
        @click="handleDownload">
        导出
      </el-button> -->
    </div>
    <el-table v-loading="listLoading" :data="list" border fit highlight-current-row style="width: 100%;">
      <el-table-column label="#" fixed="left" width="40" align="center" type="index"
        :index="(i)=>{ return (listQuery.pageNum - 1) * listQuery.pageSize + i + 1 }">
      </el-table-column>
      <el-table-column label="名称" min-width="150px">
        <template slot-scope="{row}">
          <!-- <span class="link-type" @click="handleUpdate(row)">{{ row.groupName }} </span> -->
          <!-- <el-tag>{{ row.gmtModified | dateFormat()  }}</el-tag> -->
          <template v-if="row.edit === true">
            <el-input v-model="row.groupName" size="small" clearable />
            <el-button type="text" @click="confirmEdit(row)">保存</el-button>
            <el-button type="text" @click="cancelEdit(row)">取消</el-button>
          </template>
          <span v-else class="link-type" @click="handleUpdate(row)">{{ row.groupName }} </span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="管理员" width="150px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.groupAdminUserId }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="创建时间" width="150px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.gmtCreate | dateFormat() }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" width="150px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.gmtModified | dateFormat() }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" class-name="status-col" width="100">
        <template slot-scope="{row}">
          <el-tag :type="row.isDeleted | isDeletedFilter">
            {{ row.isDeleted ? '已删除' : '正常' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="230" class-name="small-padding fixed-width">
        <template slot-scope="{row,$index}">
          <el-button type="primary" size="mini" plain @click="handleUpdate(row)">
            修改
          </el-button>
          <el-button v-if="row.isDeleted" size="mini" plain type="success" @click="handleRestoreGroup(row)">
            恢复
          </el-button>
          <el-button v-if="!row.isDeleted" size="mini" plain type="danger" @click="handleDelete(row,$index)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.pageNum" :limit.sync="listQuery.pageSize"
      @pagination="getList" />
  </div>
</template>

<script>
  import Pagination from '@/components/Pagination'
  import {
    SYS_GROUP_API
  } from '@/api/systemApi.js'

  export default {
    components: {
      Pagination
    },
    filters: {
      isDeletedFilter(isDeleted) {
        return isDeleted ? 'danger' : 'success'
      },
      statusFilter(status) {
        const statusMap = {
          published: 'success',
          draft: 'info',
          deleted: 'danger'
        }
        return statusMap[status]
      },
      typeFilter(type) {
        return calendarTypeKeyValue[type]
      }
    },
    data() {
      return {
        list: null,
        total: 0,
        listQuery: {
          pageNum: 1,
          pageSize: 20,
          isDeleted: undefined,
          groupName: undefined
        },
        listLoading: true,
        downloadLoading: false
      }
    },
    created() {
      this.handleFilter()
    },
    methods: {
      async getList() {
        this.listLoading = true
        var res = await SYS_GROUP_API.listGroups(this.listQuery);
        res.data.list.forEach(x => x.edit = false) //为修改做准备
        this.list = res.data.list
        this.total = res.data.total
        this.listLoading = false
      },
      handleIndex(index) {
        return (this.listQuery.pageNum - 1) * this.listQuery.pageSize + index + 1
      },
      handleFilter() {
        this.listQuery.pageNum = 1
        this.getList()
      },
      handleCreate() {
        this.$router.push({
          path: './create'
        })
      },
      handleUpdate(row) {
        row.originalGroupName = row.groupName;
        row.edit = true;
      },
      cancelEdit(row) {
        row.edit = false
        row.groupName = row.originalGroupName
      },
      async confirmEdit(row) {
        let params = {
          groupId: row.groupId,
          groupName: row.groupName
        }
        let res = await SYS_GROUP_API.updateGroupName(params);
        this.$message({
          message: res.message,
          type: 'success'
        })
        row.edit = false
      },
      async handleRestoreGroup(row) {
        let params = {
          groupId: row.groupId
        }
        let res = await SYS_GROUP_API.restoreGroup(params);
        this.$message({
          message: res.message,
          type: 'success'
        })
        row.isDeleted = false
      },
      handleDelete(row, index) {
        this.$confirm(`此操作将删除组织 “${row.groupName}”， 是否继续？`, '提醒', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          let params = {
            groupId: row.groupId
          };
          let res = await SYS_GROUP_API.deleteGroup(params);
          this.$message({
            type: 'success',
            message: res.message
          });
          row.isDeleted = true;
        }).catch(() => {
          // this.$message({
          //   type: 'info',
          //   message: '已取消删除'
          // });
        });
      },


    }
  }
</script>
